//
//  tools.m
//  Item
//
//  Created by Mac_Xqw on 16/1/28.
//  Copyright © 2016年 Kelly. All rights reserved.
//

#import "tools.h"
#define URL @"http://a.apix.cn/apixmoney/stockdata/stock?stockid"
#define unicodeToStr(str)

@implementation tools


+(NSString*)floatToStr:(NSString*)str{
    float floatStr = [[NSString stringWithFormat:@"%@",str] floatValue];
    return [NSString stringWithFormat:@"%.2f",floatStr];
}
+(NSString*)changeString:(NSString*)str{
    int intSt = [[NSString stringWithFormat:@"%@",str] intValue];
    intSt =  intSt/100 ;
    return [NSString stringWithFormat:@"%d",intSt];
}
+(BOOL)comparePrice:(NSString*)currPrice newNum:(NSString*)newPrize{
    float curr = [[NSString stringWithFormat:@"%@",currPrice] floatValue];
    float new = [[NSString stringWithFormat:@"%@",newPrize] floatValue];
    if (new > curr)
    {
        return YES;
    }else
    {
        return NO;
    }
    return NO;
}
+(NSString*)changeStr:(NSString*)codeStr{
    NSString* tempStr =  [codeStr substringToIndex:3];
    if ([tempStr isEqualToString:@"600"] || [tempStr isEqualToString:@"601"]  || [tempStr isEqualToString:@"900"] ) {
        return [NSString stringWithFormat:@"sh%@",codeStr];
    } else if([tempStr isEqualToString:@"000"] || [tempStr isEqualToString:@"002"] || [tempStr isEqualToString:@"200"] || [tempStr isEqualToString:@"300"]){
        return [NSString stringWithFormat:@"sz%@",codeStr];
    }else{
        return codeStr;
    }
    return nil;

}
+(NSString*)changePrice:(NSString*)str addStr:(float)num type:(NSString*)type{
    if ([type isEqualToString:@"ADD"]) {
        float floatStr = [[NSString stringWithFormat:@"%@",str] floatValue] + num;
        return [NSString stringWithFormat:@"%.2f",floatStr];
        
    } else if([type isEqualToString:@"CUT"]){
        float floatStr = [[NSString stringWithFormat:@"%@",str] floatValue] - num;
        return [NSString stringWithFormat:@"%.2f",floatStr];
    }
    return nil;
}

+(NSString*)changeCount:(NSString *)str addStr:(int)num type:(NSString *)type{
    if ([type isEqualToString:@"ADD"]) {
        long intSt = [[NSString stringWithFormat:@"%@",str] intValue] + num;
        return [NSString stringWithFormat:@"%ld",intSt];
    } else if([type isEqualToString:@"CUT"]){
        long intSt = [[NSString stringWithFormat:@"%@",str] intValue] - num;
        return [NSString stringWithFormat:@"%ld",intSt];
    }else if([type isEqualToString:@"/"]){
        long intSt = [[NSString stringWithFormat:@"%@",str] intValue] / num;
        return [NSString stringWithFormat:@"%ld",intSt];
    }
    return nil;
}

+(NSDictionary*)JSonData:(NSData*)data{
    return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
}

+(NSString*)returnPlistPath{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    
    NSString *filePath = [path stringByAppendingPathComponent:@"AllSharesCode.plist"];
    return filePath;
}

+ (NSString *)replaceUnicode:(NSString *)unicodeStr
{
    
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
    NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}

+(NSString*)returnUPOrDownStopPrice:(NSString *)str type:(NSString*)type{
    float floatStr = [[NSString stringWithFormat:@"%@",str] floatValue];
    if ([type isEqualToString:@"UP"])
        floatStr = floatStr*1.1;
    else if ([type isEqualToString:@"DOWN"]){
        floatStr = floatStr*0.9;
    }
    return [NSString stringWithFormat:@"%.2f",floatStr];
}


@end
